package com.vmladenov.cook.core.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import com.vmladenov.cook.core.Helpers;

public class DataHelper
{
	private static final String DATABASE_NAME = "cook.db";
	// private DatabaseHelper helper;
	private SQLiteDatabase db;

	private RecipesRepository recipesRepository;
	private SpicesRepository spicesRepository;
	private ProductsRepository productsRepository;
	private DictionaryRepository dictionaryRepository;
	private AdvicesRepository advicesRepository;

	public DataHelper(Context context)
	{
		// this.helper = new DatabaseHelper(context);
		// this.db = helper.getWritableDatabase();
		String path = getDbPath(context);
		this.db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
		recipesRepository = new RecipesRepository(db);
		spicesRepository = new SpicesRepository(db);
		productsRepository = new ProductsRepository(db);
		dictionaryRepository = new DictionaryRepository(db);
		advicesRepository = new AdvicesRepository(db);
	}

	private String getDbPath(Context context)
	{
		String state = Environment.getExternalStorageState();
		// If there is a storage card copy to it , if there is not copy to
		// database directory
		if (Environment.MEDIA_MOUNTED.equals(state)
				|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
		{
			String dbPath = context.getExternalFilesDir(null) + "/" + DATABASE_NAME;
			File file = new File(dbPath);
			if (!file.exists())
			{
				AssetManager manager = context.getAssets();
				try
				{
					InputStream in = manager.open(DATABASE_NAME);
					OutputStream out = new FileOutputStream(dbPath);
					Helpers.copyFile(in, out);
					in.close();
					in = null;
					out.flush();
					out.close();
					out = null;
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
			return context.getExternalFilesDir(null) + "/" + DATABASE_NAME;
		}
		else
		{
			String filePath = context.getFilesDir() + "/" + DATABASE_NAME;
			File file = new File(filePath);
			if (!file.exists())
			{

				AssetManager manager = context.getAssets();
				try
				{
					InputStream in = manager.open(DATABASE_NAME);
					OutputStream out = context.openFileOutput(DATABASE_NAME, Context.MODE_PRIVATE);
					Helpers.copyFile(in, out);
					in.close();
					in = null;
					out.flush();
					out.close();
					out = null;
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
			return file.getAbsolutePath();
		}
	}

	public void close()
	{
		getRecipesRepository().close();
		getSpicesRepository().close();
		getProductsRepository().close();
		getDictionaryRepository().close();
		getAdvicesRepository().close();
		if (db.isOpen()) db.close();
		// helper.close();
	}

	public RecipesRepository getRecipesRepository()
	{
		return recipesRepository;
	}

	public SpicesRepository getSpicesRepository()
	{
		return spicesRepository;
	}

	public ProductsRepository getProductsRepository()
	{
		return productsRepository;
	}

	public DictionaryRepository getDictionaryRepository()
	{
		return dictionaryRepository;
	}

	public AdvicesRepository getAdvicesRepository()
	{
		return advicesRepository;
	}
}
